<template>
	<view>
		<navber id="head-navber" :navbarData="navbarData"></navber>
		<view class="view-code">
			<view class="head">
				<view class="head-img">
					<image :src="userInfo.head_img ? userInfo.head_img : imgUrl + '/imgs/user/head.png'"></image>
					<view class="head-vip head-vip-open">{{ userInfo.level_name }}</view>
					<view class="head-vip-img" v-if="userInfo.member_level > 0">
						<image :src="imgUrl + '/imgs/user/vip.png'"></image>
					</view>
				</view>
				<view class="head-info">
					<view class="head-info-name" :style="'text-align:center'">
						{{ userInfo.nick_name }}
					</view>
					<view class="head-info-code">
						<view class="head-info-code-text">邀请码：</view>
						<view class="head-info-code-text1" @tap="copyCode" :data-code="userInfo.popularize_code">
							{{ userInfo.popularize_code }}
						</view>
						<view class="head-info-code-copy" @tap="copyCode" :data-code="userInfo.popularize_code">
							<image :src="imgUrl + '/imgs/user/copy.png'"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="list">
			<view class="info">

			</view>
			<view class="qrcode">
				<img :src="imgUrl+''+userInfo.popUrl" alt="">
				<!-- <canvas canvas-id="qrcode" v-show="qrShow" style="height: 500rpx;width: 500rpx;margin: 0 auto;"/> -->
			</view>
			<view class="save-btn" @click="onSaveImg()">点击保存图片</view>
		</view>
	</view>
</template>

<script>
	import navber from '@/components/navber/index';
	const Config = require('../../../config/index.js');
	const Api = require('../../../api/index.js');
	const app = getApp();
	export default {
		components: {
			navber
		},
		data() {
			return {
				navbarData: {
					title: '推广码' //顶部标题
				},
				//导航栏
				imgUrl: Config.imgUrl,
				qrShow: false,
				userInfo: {
					popularize_code: "",
					member_level: "",
					popUrl: ""
				}
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			this.getData();
		},
		methods: {
			getData() {
				Api.userCore({
					type: 'pop'
				}).then((res) => {
					if (res.code == 1 && res.data) {
						this.setData({
							userInfo: res.data.userInfos,
						});

					}
				});
			},
			copyCode(e) {
				//复制推广码
				// console.log(e.currentTarget.dataset.code);
				const code = e.currentTarget.dataset.code;
				uni.setClipboardData({
					data: code,
					success(res) {
						uni.getClipboardData({
							success(res) {
								console.log(res.data); // data
							}
						});
					}
				});
			},
			onSaveImg() {
				let that = this;
				uni.getSetting({
					success: function(res) {
				  if (res.authSetting['scope.writePhotosAlbum'] != undefined && res.authSetting[
								'scope.writePhotosAlbum'] != true) {
				   uni.showModal({
								title: '请授权保存到相册',
								content: '需要保存到相册权限，请确认授权',
								success: function(res) {
									if (res.cancel) {
										uni.showToast({
											title: '取消授权',
											icon: 'none',
											duration: 1000
										});
									} else if (res.confirm) {
										uni.openSetting({
											success: function(dataAu) {
												if (dataAu.authSetting[
														'scope.writePhotosAlbum'] ==
													true) {
													uni.showToast({
														title: '授权成功',
														icon: 'success',
														duration: 1000
													});
													that.authSaveImg();
												} else {
													uni.showToast({
														title: '授权失败',
														icon: 'none',
														duration: 1000
													});
												}
											}
										});
									}
								}
							});
				 	} else if (res.authSetting['scope.writePhotosAlbum'] == undefined) {
							that.authSaveImg();
						} else {
							that.authSaveImg();
						}
					},

					fail(res) {}
				});
			},
			//授权
			authSaveImg: function() {
				let that = this;
				uni.authorize({
			 	scope: 'scope.writePhotosAlbum',
					success: function(res) {
						let imgurl = that.imgUrl+''+that.userInfo.popUrl;
						console.log(imgurl);
						uni.downloadFile({
							url: imgurl,
							success: function(res) {
								uni.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success: function(res) {
										uni.showToast({
											title: '保存成功'
			  					});
										that.shareSuceese();
									}
								});
							}
						});
					},

					fail(res) {
						console.log(res);
						uni.showToast({
							title: '授权失败',
							icon: 'none'
						});
					}
				});
			}
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {
			const app = getApp();
			// console.log(app.globalData.popularize_code);
			var rand = Math.random() * 100;
			return {
				title: app.globalData.title + '：更多优惠的商品等您来选购哦！',
				path: '/pages/index/index?code=' + this.userInfo.popularize_code,
				imageUrl: Config.imgUrl + '/imgs/share.png?rand=' + rand
			};
		},
		onShareTimeline: function() {
			const app = getApp();
			// console.log(app.globalData.popularize_code);
			var rand = Math.random() * 100;
			return {
				title: app.globalData.title + '：更多优惠的商品等您来选购哦！',
				path: '/pages/index/index?code=' + this.userInfo.popularize_code,
				imageUrl: Config.imgUrl + '/imgs/share.png?rand=' + rand
			};
		}
	};
</script>

<style>
	@import './index.css';
</style>
